<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of lock_obj
 *
 * @author Juraj
 */
class lock_obj {

    public function getLocks($filter = array()) {

        $db_obj = new db_obj();

        $where = 'WHERE 1=1';

        foreach ($filter as $key => $value) {
            $where .= " AND " . $key . "='" . $value . "'";
        }
        
        $sql = "SELECT * FROM " . $GLOBALS['tb_locks'] . " " . $where;
        
        return $db_obj->getAll($sql);
    }
    
    public function addLock($in_data) {
        if (empty($in_data)) {
            return new error_obj('addLock -> empty $in_data', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $result = $db_obj->insert($GLOBALS['tb_locks'], $in_data);
        if (error_obj::isError($result))
            return $result;
        else
            return $db_obj->lastInsertedId();
    }
    
    public function deleteLockRow($month, $year, $type = '') {
        $m = (int) $month;
        if (empty($m)){
            return new error_obj('deleteLock -> empty $m', get_class(), __FUNCTION__);
        }
        
        $y = (int) $year;
        if (empty($y)){
            return new error_obj('deleteYear -> empty $y', get_class(), __FUNCTION__);
        }
        
        if (!empty($type)){
            $type = " AND type='".$type."'";
        }
        $db_obj = new db_obj();
        
        $sql = "DELETE FROM " . $GLOBALS['tb_locks'] . " WHERE month=$m AND year=$y $type";
        
        $result = $db_obj->query($sql);
        return $result;
    }
    
    public function isRecordLocked($date){
        $date = strip_tags($date);
        if (empty($date))
            return true;
        
        $date = explode('/', $date);
        
        $filter = array(
            'month' => $date[1],
            'year' => $date[2],
            'type' => 'record',
        );
        
        $result = $this->getLocks($filter);
        if (error_obj::isError($result) || !empty($result))
            return true;
        
        return false;
        
    }
    
    public function isEntryLocked($date){
        $date = strip_tags($date);
        if (empty($date))
            return true;
        
        $date = explode('/', $date);
        
        $filter = array(
            'month' => $date[1],
            'year' => $date[2],
            'type' => 'cash',
        );
        
        $result = $this->getLocks($filter);
        if (error_obj::isError($result) || !empty($result))
            return true;
        
        return false;
        
    }

}

?>
